import requests
from lxml import etree
import re
import time
import pymysql

#获取医院链接
def get_hospital_link(page):
	url = 'http://yyk.39.net/hospitals/c_p{}/'.format(str(page))
	print(url)
	headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'}
	htm = requests.get(url,headers=headers).text
	links = re.findall(r"getHospitalLikes\('(.*?)'\)",htm)
	#print(links)
	return links
#获取医院信息
def get_hospital_info(code):
	headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'}
	time.sleep(1)
	htm = etree.HTML(requests.get('http://yyk.39.net/hospital/{}_detail.html'.format(code),headers=headers).text) #详细介绍界面
	time.sleep(1)
	html = etree.HTML(requests.get('http://yyk.39.net/hospital/{}_labs.html'.format(code),headers=headers).text) #科室界面
	time.sleep(1)
	#医院名称
	try:
		hospital_name = htm.xpath('/html/body/div[@class="jy_hspt_mid"]/div[@class="wid1000"]/div[@class="jy_hspt_intro"]/div[@class="jy_hspt_intro_m"]/div[@class="l"]/h2/text()')[0]
	except:
		return 0
	#医院等级
	try:
		hospital_grade = str(htm.xpath('/html/body/div[@class="jy_hspt_mid"]/div[@class="wid1000"]/div[@class="jy_hspt_intro"]/div[@class="jy_hspt_intro_m"]/div[@class="l"]/h2/span/i/text()')).replace('[','').replace(']','').replace('\'','').replace(' ','').replace('[]','')
	except:
		hospital_grade = None
	#医院简介
	try:
		hospital_introduction = str(htm.xpath('/html/body/div[@class="jy_hspt_mid"]/div[@class="wid1000"]/div[@class="jy_hspt_main"]/div[@class="jy_hspt_main_l"]/div[@class="hos-intro"]/div[@class="con hos-intro-sur"]/dl[@class="sur-info2 clearfix"]/dd/p/text()')).replace("', '","\n").replace('[\'','').replace('\']','').replace('[]','').replace('\xa0','')
	except:
		hospital_introduction = None
	#医院地址
	try:
		hospital_address = htm.xpath('/html/body/div[@class="jy_hspt_mid"]/div[@class="wid1000"]/div[@class="jy_hspt_main"]/div[@class="jy_hspt_main_l"]/div[@class="hos-intro"]/div[@class="con hos-intro-sur"]/dl[@class="sur-info clearfix"]/dd/p/span/text()')[-1]
	except:
		hospital_address = None
	#医院电话
	try:
		hospital_phone = str(htm.xpath('/html/body/div[@class="jy_hspt_mid"]/div[@class="wid1000"]/div[@class="jy_hspt_main"]/div[@class="jy_hspt_main_l"]/div[@class="hos-intro"]/div[@class="con hos-intro-sur"]/dl[@class="sur-info clearfix"]/dd/p/span/text()')[:-1]).replace('[\'','').replace('\']','').replace(' ','').replace("','",'').replace('\\','').replace('t','').replace('r','').replace('n','').replace('[]','')
	except:
		hospital_phone = None
	#科室数量
	try:
		hospital_department_num = html.xpath('/html/body/div[@class="jy_hspt_mid"]/div[@class="wid1000"]/div[@class="jy_hspt_main"]/div[@class="jy_hspt_main_l"]/div[@class="keshi-left"]/div[@class="keshi-left-lab"]/div[@class="keshi-left-title"]/div[@class="count"]/b/text()')[0]
	except:
		hospital_department_num = None
	#医生数量
	try:
		hospital_doctor_num = html.xpath('/html/body/div[@class="jy_hspt_mid"]/div[@class="wid1000"]/div[@class="jy_hspt_main"]/div[@class="jy_hspt_main_l"]/div[@class="keshi-left"]/div[@class="keshi-left-lab"]/div[@class="keshi-left-title"]/div[@class="count"]/b/text()')[1]
	except:
		hospital_doctor_num = None
	#床位数量
	try:
		hospital_bed_num = htm.xpath('/html/body/div[@class="jy_hspt_mid"]/div[@class="wid1000"]/div[@class="jy_hspt_main"]/div[@class="jy_hspt_main_l"]/div[@class="hos-intro"]/div[@class="con hos-intro-sur"]/ul[@class="sur-data clearfix"]/li/i/text()')[-2]
	except:
		hospital_bed_num = None
	#科室
	try:
		hospital_department = str(html.xpath('/html/body/div[@class="jy_hspt_mid"]/div[@class="wid1000"]/div[@class="jy_hspt_main"]/div[@class="jy_hspt_main_l"]/div[@class="keshi-left"]/div[@class="keshi-left-lab"]/div[@class="cons"]/div[@class="lab-list"]/div/ul/li/a/text()')).replace('[\'','').replace('\']','').replace(' ','').replace('\',\'','|').replace('[]','') 
	except:
		hospital_department = None
	#门诊数量
	try:
		hospital_outpatient_num = htm.xpath('/html/body/div[@class="jy_hspt_mid"]/div[@class="wid1000"]/div[@class="jy_hspt_main"]/div[@class="jy_hspt_main_l"]/div[@class="hos-intro"]/div[@class="con hos-intro-sur"]/ul[@class="sur-data clearfix"]/li/i/text()')[-1]
	except:
		hospital_outpatient_num = None
	return hospital_name,hospital_grade,hospital_introduction,hospital_address,hospital_phone,hospital_department_num,hospital_doctor_num,hospital_bed_num,hospital_department,hospital_outpatient_num
def get_save(info):
	cur.execute("INSERT into hospital (hospital_name, hospital_grade, hospital_introduction, hospital_address, hospital_phone, hospital_department_num, hospital_doctor_num, hospital_bed_num, hospital_department, hospital_outpatient_num) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(info[0],info[1],info[2],info[3],info[4],info[5],info[6],info[7],info[8],info[9]))
	conn.commit()
	print(page,info[0])
if __name__ == '__main__':
	print(get_hospital_link(1))
	'''
	conn = pymysql.connect(host="localhost",user="root",passwd="111111",db="hospital",charset="utf8")
	cur = conn.cursor()
	cur.execute("use hospital")
	conn.commit()
	for page in range(3,1001):
		for code in get_hospital_link(page):
			try:
				info = get_hospital_info(code)
				if info == 0:
					continue
				else:
					get_save(info)
			except:
				continue
	conn.close()
	cur.close()
	'''